import Vue from 'vue'

import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({

  state: {
    loading: false,
    user: {
      nickName: '' || localStorage.getItem('nickName'),
      avatar: '' || localStorage.getItem('avatar'),
      sign: '' || localStorage.getItem('sign')
    }

    // 这里放全局参数

  },

  mutations: {
    CHANGE_LOADING (state, loading) {
      state.loading = loading
    },
    SET_USER_INFO (state, user) {
      state.user = user
      console.log('------SET_USER_INFO--------')
      console.log(state.user)
    }
    // 这里是set方法

  },

  getters: { // 这里是get方法
    getNickName: state => state.user.nickName,
    getSign: state => state.user.sign,
    getAvatar: state => state.user.avatarUrl,
    getUserPhone: state => state.user.phone,
    getUserId: state => state.user.id,
    isLoading: state => state.loading
  },

  actions: {
    changeLoading ({ commit }, loading) {
      commit('CHANGE_LOADING', loading)
    },
    setUserInfo ({ commit }, user) {
      console.log('------setUserInfo--------')
      commit('SET_USER_INFO', user)
    }

    // 这个部分我暂时用不上

  },

  modules: {

    // 这里是我自己理解的是为了给全局变量分组，所以需要写提前声明其他store文件，然后引入这里

  }

})
